import { ProductPageQuery } from '../dtos';
import { entityToDto } from '../assembler/productAssembler';
import { ProductRepository } from '../repositories/productRepository';
import { getCustomRepository } from 'typeorm';

export interface IProductService {
  findForPage(pageQuery: ProductPageQuery): Promise<any>;
}

export class ProductService implements IProductService {

  private productRepository: ProductRepository;

  constructor() {
    this.productRepository = getCustomRepository(ProductRepository);
  }

  async findForPage(pageQuery: ProductPageQuery): Promise<any> {
    const pageData = await this.productRepository.findForPage(pageQuery);
    pageData.data = pageData.data.map(job => entityToDto(job));
    return pageData;
  }

  async findAll(): Promise<any> {
    const products = await this.productRepository.find();
    return products.map(product => entityToDto(product));
  }

}
